package com.example.black.myapplication.materia;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.black.myapplication.R;

public class DrawLayoutDemoActivity extends AppCompatActivity {

    private static final String TAG = DrawLayoutDemoActivity.class.getName();
    private DrawerLayout draw;
    private Button btn;
    private Button btn2;
    private Toolbar toolbar;
    private ActionBarDrawerToggle drawerToggle;
    private View container;
    private View left;
    private Button startNv;
    private Button startSnackBar;
    private  Snackbar snackbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_draw_layout_demo);
        findViewById(R.id.btn2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                draw.closeDrawer(Gravity.LEFT);
            }
        });
        initView();
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // 需要将ActionDrawerToggle与DrawerLayout的状态同步
        // 将ActionBarDrawerToggle中的drawer图标，设置为ActionBar中的Home-Button的Icon
        drawerToggle.syncState();
    }

    private void initView() {
        toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        draw = findViewById(R.id.draw);
        btn = findViewById(R.id.btn);
        drawerToggle = new ActionBarDrawerToggle(
                this,
                draw
                , toolbar
                , R.string.drawer_open
                , R.string.drawer_close
        );
        left = findViewById(R.id.leftLayout);
        container = findViewById(R.id.container_layout);
        draw.addDrawerListener(drawerToggle);
        draw.addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {

                DrawerLayout.LayoutParams layoutParams = (DrawerLayout.LayoutParams) drawerView.getLayoutParams();
                if (layoutParams.gravity == Gravity.START) {
//                 left.setScaleX(leftScale);
//                 left.setScaleY(leftScale);
                    container.setTranslationX(left.getMeasuredWidth() * slideOffset);//0~width
                } else {
                    container.setTranslationX(-1 * left.getMeasuredWidth() * slideOffset);//0~width
                }
            }

            @Override
            public void onDrawerOpened(@NonNull View drawerView) {

            }

            @Override
            public void onDrawerClosed(@NonNull View drawerView) {

            }

            @Override
            public void onDrawerStateChanged(int newState) {

            }
        });
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                draw.openDrawer(Gravity.LEFT);
            }
        });
        btn2 = findViewById(R.id.btn2);
        startNv = (Button) findViewById(R.id.startNv);
        startNv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(
                        DrawLayoutDemoActivity.this
                        , NavigationActivity.class
                ));
            }
        });

        startSnackBar = (Button) findViewById(R.id.startSnackBar);
         snackbar =  Snackbar.make(toolbar, "提示消息", Snackbar.LENGTH_LONG);
        snackbar    .setAction("按钮", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点击右侧的按钮之后的操作
            }
        });
        snackbar.setActionTextColor(getResources().getColor(R.color.colorAccent));
        snackbar.addCallback(new Snackbar.Callback(){
            @Override
            public void onShown(Snackbar sb) {
                super.onShown(sb);
            }

            @Override
            public void onDismissed(Snackbar transientBottomBar, int event) {
                super.onDismissed(transientBottomBar, event);
                makeText();
            }
        });
        startSnackBar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                snackbar.show();
            }
        });
    }

    private void makeText() {
      Context context =  DrawLayoutDemoActivity.this ;
        Toast result = new Toast(DrawLayoutDemoActivity.this.getBaseContext());

        LayoutInflater inflate = (LayoutInflater)
                context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflate.inflate(R.layout.navigation_headerlayout, null);
        result.setView(v);
        result.setDuration(Toast.LENGTH_LONG);
        result.show();
    }
}
